package com.lebang.medicalInquiry.controller;

import com.lebang.medicalInquiry.component.TokenComponent;
import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.constant.annotation.RateLimit;
import com.lebang.medicalInquiry.constant.annotation.Require;
import com.lebang.medicalInquiry.model.po.PhysicianCase;
import com.lebang.medicalInquiry.model.vo.PhysicianInfoVo;
import com.lebang.medicalInquiry.model.vo.SubjectClassifyVo;
import com.lebang.medicalInquiry.model.vo.UserTokenVo;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.PhysicianService;
import com.lebang.medicalInquiry.service.UserService;
import com.lebang.medicalInquiry.utils.AuthLiveUrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/api/physician")
@Api(tags = "医师相关")
@RestController
@Validated
public class PhysicianApi {

    @Autowired
    private PhysicianService physicianService;

    @Autowired
    private TokenComponent tokenComponent;

    @Autowired
    private UserService userService;


    @GetMapping("/classify")
    @ApiOperation("查询分类标签")
    public JsonData<List<SubjectClassifyVo>> getSubjectClassifyList(HttpServletRequest request, HttpServletResponse response,
                                                                    @ApiParam(name = "isPhysician", value = "是否为医师的分类标签 0 否 1 是")
                                                                    @RequestParam(value = "isPhysician", required = false, defaultValue = "1") Integer isPhysician) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), physicianService.getSubjectClassifyList(isPhysician));

    }

    @GetMapping("/")
    @Require
    @ApiOperation("下滑返回医生视频")
    @RateLimit(key = "PhysicianList:", capacity = 3, replenishRate = 3)
    public JsonData<List<PhysicianInfoVo>> getAttPhysicianList(HttpServletRequest request, HttpServletResponse response,
                                                               @ApiParam(name = "name", value = "医生名称")
                                                               @RequestParam(value = "name", required = false) String name,
                                                               @ApiParam(name = "isAttention", value = "是否关注", required = true) @NotNull(message = "isAttention 必传")
                                                               @RequestParam(value = "isAttention", required = false) Integer isAttention,
                                                               @ApiParam(name = "classifyId", value = "学科分类id")
                                                               @RequestParam(value = "classifyId", required = false) Integer classifyId,
                                                               @ApiParam(name = "organizationId", value = "机构id")
                                                               @RequestParam(value = "organizationId", required = false) Integer organizationId,
                                                               @ApiParam(name = "page", value = "页码")
                                                               @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                               @ApiParam(name = "limit", value = "条数")
                                                               @RequestParam(value = "limit", required = false, defaultValue = "1") Integer limit) {
//        String uuid = tokenComponent.getMember(request).getUuid();
        String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), userService.getAttPhysicianList(name, uuid, classifyId, organizationId, isAttention, page, limit));

    }

    @RateLimit(key = "attention:", capacity = 1)
    @GetMapping("/attention/{physicianId}")
    @Require
    @ApiOperation("关注或者取关医师")
    public JsonData attentionPhysician(HttpServletRequest request, HttpServletResponse response,
                                       @ApiParam(name = "physicianId", value = "医师id", required = true) @NotNull(message = "physicianId 不能为空")
                                       @PathVariable(value = "physicianId")
                                               Integer physicianId,
                                       @ApiParam(name = "attentionState", value = "0 取关 1 关注", required = true) @NotNull(message = "attentionState 不能为空")
                                       @RequestParam(value = "attentionState", required = false)
                                               Integer attentionState) {
        UserTokenVo member = tokenComponent.getMember(request);
        physicianService.attentionPhysician(member.getUuid(), physicianId, attentionState);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

//    @GetMapping("/list")
//    @ApiOperation("查询所以可用的医师")
//    public JsonData<List<Physician>> getPhysicianList(HttpServletRequest request, HttpServletResponse response) {
//
//
//        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), physicianService.getPhysicianList());
//
//    }


    @GetMapping("/info/{physicianId}")
    @ApiOperation("根据id查询详细信息")
    public JsonData<PhysicianInfoVo> getPhysicianInfo(HttpServletRequest request, HttpServletResponse response,
                                                      @ApiParam(name = "physicianId", value = "医师id") @NotNull(message = "physicianId 不能为空") @PathVariable(value = "physicianId")
                                                              Integer physicianId
    ) {

        PhysicianInfoVo articlesInfo = physicianService.getPhysicianInfo(physicianId);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), articlesInfo);

    }


    @GetMapping("/put/stream/url/{physicianId}")
    @ApiOperation("获取拉流链接")
    public JsonData<Object> getPutStreamUrl(HttpServletRequest request, HttpServletResponse response,
                                            @ApiParam(name = "physicianId", value = "医师id") @NotNull(message = "physicianId 不能为空") @PathVariable(value = "physicianId")
                                                    Integer physicianId) {
        PhysicianInfoVo articlesInfo = physicianService.getPhysicianInfo(physicianId);
//        String url = AuthLiveUrlUtil.aAuth("http://paylive.comicani.com/live/test.flv", "cPRPlpVd2S3Om2Si", articlesInfo.getId() + "", System.currentTimeMillis() / 1000 + 1 * 3600);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), articlesInfo.getPullStreamUrl());

    }

    @GetMapping("/case/{physicianId}")
    @ApiOperation("获取病人案例")
    public JsonData<List<PhysicianCase>> getPhysicianCaseList(HttpServletRequest request, HttpServletResponse response,
                                                              @ApiParam(name = "physicianId", value = "医师id") @NotNull(message = "physicianId 不能为空") @PathVariable(value = "physicianId")
                                                                      Integer physicianId,
                                                              @ApiParam(name = "type", value = "0 医师 1 机构") @NotNull(message = "type 不能为空") @RequestParam(value = "type", required = false, defaultValue = "0")
                                                                      Integer type) {

        List<PhysicianCase> list = physicianService.getPhysicianCaseList(physicianId, type);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);

    }

    @Require
    @PostMapping("/head/img")
    @ApiOperation("获取医师的头像")
    public JsonData getUserHeadImg(HttpServletRequest request, HttpServletResponse response,
                                   @ApiParam(name = "sheetId", value = "问诊单的id") @RequestBody List<Integer> ids) {
        String uuid = request.getAttribute("uuid").toString();
        if (ids.size() == 0) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), "医师列表不能为空");
        }
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), physicianService.getHeadImg(ids));

    }
//    @GetMapping("/organization/{physicianId}")
//    @ApiOperation("查看当前医师支持的担保机构列表")
//    public JsonData<List<OrganizationVo>> getOrganizationList(HttpServletRequest request, HttpServletResponse response,
//                                                              @ApiParam(name = "physicianId", value = "医师id") @NotNull(message = "physicianId 不能为空") @PathVariable(value = "physicianId")
//                                                            Integer physicianId) {
//
//        List<OrganizationVo> list = physicianService.getOrganizationList(physicianId);
//
//        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);
//
//    }
//
//    @GetMapping("/organization/info/{organizationId}")
//    @ApiOperation("查询某个机构的相关主页信息")
//    public JsonData<Organization> getOrganizationInfo(HttpServletRequest request, HttpServletResponse response,
//                                                      @ApiParam(name = "organizationId", value = "机构id") @NotNull(message = "organizationId 不能为空") @PathVariable(value = "organizationId")
//                                                      Integer organizationId) {
//
//        Organization info = physicianService.getOrganizationInfo(organizationId);
//
//        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), info);
//
//    }
}
